﻿using System;
using System.Collections.Generic;

namespace NPlurk
{
    public class User
    {
        /// <summary>
        /// The unique user id.
        /// </summary>
        public long id { get; set; }

        /// <summary>
        /// The unique nick_name of the user, for example amix.
        /// </summary>
        public string nick_name { get; set; }

        /// <summary>
        /// The non-unique display name of the user, for example Amir S. Only set if it's non empty.
        /// </summary>
        public string display_name { get; set; }

        /// <summary>
        /// The user's full name, like Amir Salihefendic.
        /// </summary>
        public string full_name { get; set; }

        /// <summary>
        /// The user's birthday.
        /// </summary>
        public DateTime? date_of_birth { get; set; }

        /// <summary>
        /// 1 is male, 0 is female, 2 is not stating/other.
        /// </summary>
        public enum plurk_sex { female = 0, male = 1, others = 2 }
        public plurk_sex gender { get; set; }

        /// <summary>
        /// Can be not_saying, single, married, divorced, engaged, in_relationship, complicated, widowed, open_relationship
        /// </summary>
        public string relationship { get; set; }

        /// <summary>
        /// The user's location, a text string, for example Aarhus Denmark.
        /// </summary>
        public string location { get; set; }

        /// <summary>
        /// The profile title of the user.
        /// </summary>
        public string page_title { get; set; }

        /// <summary>
        /// User's karma value.
        /// </summary>
        public Single karma { get; set; }

        /// <summary>
        /// Specifies what the latest avatar (profile picture) version is.
        /// </summary>
        public int? avatar { get; set; }

        /// <summary>
        /// How many friends has the user recruited.
        /// </summary>
        public int recruited { get; set; }

        /// <summary>
        /// If 1 then the user has a profile picture, otherwise the user should use the default.
        /// </summary>
        public int? has_profile_image { get; set; }

        public int? profile_views { get; set; }
        public bool settings { get; set; }
        public bool is_premium { get; set; }

        public bool following { get; set; }
        public string timezone { get; set; }
        public int? last_logged_in { get; set; }
        public int? num_of_followers { get; set; }


        public enum avatar_size { small, medium, big }

        public string avatar_url(avatar_size size) 
        {
            return get_avatar_url(size.ToString());
        }
        public string avatar_small { get { return get_avatar_url("small"); } }
        public string avatar_medium { get { return get_avatar_url("medium"); } }
        public string avatar_big { get { return get_avatar_url("big"); } }

        private string get_avatar_url(string size)
        {
            if (has_profile_image == 0)
            {
                return string.Format("http://www.plurk.com/static/default_{0}.jpg", size);
            }

            var ext = has_profile_image.HasValue && size == "big" ? "jpg" : "gif";
            if (avatar.HasValue)
            {
                return string.Format("http://avatars.plurk.com/{0}-{1}{2}.{3}", id, size, avatar, ext);
            }
            else
            {
                return string.Format("http://avatars.plurk.com/{0}-{1}.{2}", id, size, ext);
            }
        }

        public override string ToString()
        {
            return display_name ?? (full_name ?? nick_name);
        }

    }

    public class KarmaStats 
    {
        public List<string> karma_trend { get; set; }
        public string karma_fall_reason { get; set; }
        public Single current_karma { get; set; }
        public Uri karma_graph { get; set; }
    }


    public class UserChannel
    {
        public string comet_server { get; set; }
        public string channel_name { get; set; }
    }

    public class Profile
    {
        public int friends_count { get; set; }
        public int fans_count { get; set; }
        public int unread_count { get; set; }
        public int alerts_count { get; set; }
        public User user_info { get; set; }
        public string privacy { get; set; }
        public Dictionary<string, User> plurks_users { get; set; }
        public Plurk[] plurks { get; set; }
    }

    public class UnreadStats
    {
        public int all { get; set; }
        public int my { get; set; }
        public int @private { get; set; }
        public int responded { get; set; }
    }

    public class PlurkError
    {
        public string error_text { get; set; }
    }

    public class PlurkResult
    {
        public string success_text { get; set; }
    }

    public class Plurk
    {
        public long id { get; set; }
        public long plurk_id { get; set; }
        public int plurk_type { get; set; }
        public long user_id { get; set; }
        public long owner_id { get; set; }
        public int responses_seen { get; set; }
        public int response_count { get; set; }
        public byte no_comments { get; set; }
        public byte is_unread { get; set; }
        public string qualifier { get; set; }
        public string qualifier_translated { get; set; }
        public string limited_to { get; set; }
        public string lang { get; set; }
        public string content_raw { get; set; }
        public string content { get; set; }
        public DateTime posted { get; set; }

        public int favorite_count { get; set; }
        public string[] favorers { get; set; }
        public bool favorite { get; set; }

        public string GetPlurkUrl()
        {
            return string.Format("http://www.plurk.com/p/{0}", Base36.Encode(plurk_id));
        }

        public override string ToString()
        {
            return string.Format("[{0:yyyy/MM/dd HH:mm:ss}] {1} {2}: {3}", 
                posted, 
                user_id, 
                qualifier ?? qualifier_translated, 
                content_raw);
        }
    }

    public class Plurks
    {
        public Plurk[] plurks { get; set; }
        public Dictionary<string, User> plurk_users { get; set; }
    }

    public class UserPlurk
    {
        public Plurk plurk { get; set; }
        public User user { get; set; }
    
        public override string ToString()
        {
            return string.Format("[{0}] {1} {2}: {3}", 
                plurk.posted.ToString("yyyy/MM/dd HH:mm:ss"),
                user.display_name ?? user.nick_name, 
                plurk.qualifier ?? plurk.qualifier_translated, 
                plurk.content_raw);
        }
    }

    public class Responses
    {
        public Dictionary<string, User> friends { get; set; }
        public int responses_seen { get; set; }
        public Plurk[] responses { get; set; }
    }

    public class ConciseUser
    {
        public string nick_name { get; set; }
        public string display_name { get; set; }
        public string full_name { get; set; }

        public override string ToString()
        {
            return display_name ?? (full_name ?? nick_name);
        }
    }

    public class SearchResult
    {
        public bool has_more { get; set; }
        public string error { get; set; }
        public int? last_offset { get; set; }
        public Dictionary<string, User> users { get; set; }
        public Plurk[] plurks { get; set; }
    }

    public class SearchUserResult
    {
        public int counts { get; set; }
        public User[] users { get; set; }
    }

    public class Emoticons
    {
        public Dictionary<string, List<List<string>>> karma { get; set; }
        public Dictionary<string, List<List<string>>> recuited { get; set; }
    }

}